package com.tlb.dto;

import javax.persistence.*;
import java.util.*;
import java.io.*;
import com.tlb.web.Util;


@Entity
public class PostView implements Serializable {


    @Transient
    private static Util util = Util.getUtil();

    public PostView() {
    }

    public int hashCode() {
        return postId;
    }

    @Id
    private int postId;
    private int threadId;
    private int treeId;
    private int depth;
    private String monikerName;
    private int monikerId;
    private boolean showCounter;
    private String tagLine;
    private int postCount;
    private String title;
    private String content;

    @Temporal(TemporalType.TIMESTAMP)
    private Date postDate;


    private int lft;
    private int rgt;
    private int posterId;
    private boolean nsfw;
    private boolean spam;
    private boolean tos;

    public int getPostId() {
        return postId;
    }

    public void setPostId(int postId) {
        this.postId = postId;
    }

    public int getDepth() {
        return depth;
    }

    public void setDepth(int depth) {
        this.depth = depth;
    }

    public String getMonikerName() {
        return monikerName;
    }

    public void setMonikerName(String monikerName) {
        this.monikerName = monikerName;
    }

    public String getTagLine() {
        return tagLine;
    }

    public void setTagLine(String tagLine) {
        this.tagLine = tagLine;
    }

    public int getPostCount() {
        return postCount;
    }

    public void setPostCount(int postCount) {
        this.postCount = postCount;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getLft() {
        return lft;
    }

    public void setLft(int lft) {
        this.lft = lft;
    }

    public int getRgt() {
        return rgt;
    }

    public void setRgt(int rgt) {
        this.rgt = rgt;
    }

    public Date getPostDate() {
        return postDate;
    }

    public String getPostDateFmt() {
        return util.getPostViewDateFormat().format(postDate);
    }


    public void setPostDate(Date postDate) {
        this.postDate = postDate;
    }

    public int getMonikerId() {
        return monikerId;
    }

    public void setMonikerId(int monikerId) {
        this.monikerId = monikerId;
    }

    public int getThreadId() {
        return threadId;
    }

    public void setThreadId(int threadId) {
        this.threadId = threadId;
    }

    public int getPosterId() {
        return posterId;
    }

    public void setPosterId(int posterId) {
        this.posterId = posterId;
    }

    public boolean getNsfw() {
        return nsfw;
    }

    public void setNsfw(boolean nsfw) {
        this.nsfw = nsfw;
    }

    public boolean getSpam() {
        return spam;
    }

    public void setSpam(boolean spam) {
        this.spam = spam;
    }

    public boolean getTos() {
        return tos;
    }

    public void setTos(boolean tos) {
        this.tos = tos;
    }

    public int getTreeId() {
        return treeId;
    }

    public void setTreeId(int treeId) {
        this.treeId = treeId;
    }


    public String getHidenReason() {
        if(this.getTos())
            return "TOS Violation";
        else if(this.getSpam())
            return "Spam";
        else if(this.getNsfw())
            return "Not Safe For Work";
        else
            return null;
    }

    public String getHidenReasonCSSClass(Poster p) {

        //if(p==null || p.getUserLevelId() != 1)
        //    return "";

        if(this.getTos())
            return "tosBG";
        else if(this.getSpam())
            return "spamBG";
        else if(this.getNsfw())
            return "nsfwBG";
        else
            return "";
    }

    public boolean isHidden(Poster p) {
        if(p == null) //poster not logged in
            return (this.getNsfw() || this.getSpam() || this.getTos());

        else if(p.getUserLevelId()!=1 && this.getTos())
            return true;

        else if(!p.getNsfw() && this.getNsfw())
                return true;
        else if(!p.getSpam() && this.getSpam())
            return true;
        else
            return false; //admin can always see TOS
    }

    public String getContentFormatted(Poster p) {
        String retVal = "";
        try {
            if (this.isHidden(p)) {
                retVal = "Content Flagged as: " +  this.getHidenReason() + " - Register and opt in to view (Except TOS Violations)<br>";
            } else {
                if(this.getContent() != null)
                    retVal = this.getContent().replaceAll("\n", "<p>").replaceAll("(?i)([fh]t+ps?://[^ )\"'\n\r]+)", "<a href='$1' target=new>$1</a>");
            }
        } catch(Exception e) {}

        return retVal;
    }

    public String getContentTitle(Poster p) {
        String hdrCont = "";
        try {

            hdrCont = this.getContent();

            if (this.isHidden(p)) {
                hdrCont = this.getHidenReason();
            }


            if (hdrCont == null || hdrCont.trim().equals("")) {
                hdrCont = "...";
            }
            hdrCont = hdrCont.replaceAll("</?i>", "");
            hdrCont = hdrCont.length() > 50 ? hdrCont.substring(0, 49) + "..." : hdrCont;


        } catch(Exception e) {}

        return hdrCont;
    }

    public boolean getShowCounter() {
        return showCounter;
    }

    public void setShowCounter(boolean showCounter) {
        this.showCounter = showCounter;
    }

}
